//
//  WebImageViewController.m
//  Focus
//
//  Created by 楼主 on 14-11-7.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WebImageViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ImageTableViewCell.h"
#import "NetHandler.h"
#import "webObject.h"
#import "discoverTopCell.h"
@interface WebImageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic, retain)UIButton *button_back;

@property (nonatomic, retain)NSMutableDictionary *dicData;
@property (nonatomic, retain)NSMutableArray *articlesArr;
@property (nonatomic, retain)NSMutableArray *pagesArr;
@property (nonatomic, assign)NSInteger height;
@property (nonatomic, retain)NSMutableArray *dataArray;

@end

@implementation WebImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.dicData = [NSMutableDictionary dictionary];
        self.articlesArr = [NSMutableArray array];
        self.pagesArr = [NSMutableArray array];
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [NetHandler networkWithURL:self.api_url comaletion:^(id result) {
        //        NSLog(@"&*&*&*&   %@",self.api_url);
        self.dicData = result;
        self.articlesArr = [[self.dicData objectForKey:@"data"] objectForKey:@"articles"];
        self.pagesArr = [[[self.dicData objectForKey:@"data"] objectForKey:@"ipadconfig"] objectForKey:@"pages"];
        
        for (NSDictionary *dic in self.articlesArr) {
            webObject *web = [[webObject alloc]init];
            web.title = [dic objectForKey:@"title"];
            web.auther_name = [dic objectForKey:@"auther_name"];
            web.myImageView = [[dic objectForKey:@"media"][0] objectForKey:@"url"];
            [self.dataArray addObject:web];
        }
        
        [self.tableView reloadData];
        [self setupSubViews];
    }];
}



- (void)setupSubViews
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 13)];
    NSString *str = [[self.pagesArr[1] objectForKey:@"diy"] objectForKey:@"bgimage_url"];
    [imageView sd_setImageWithURL:[NSURL URLWithString:str]];
    
    [self.view addSubview:imageView];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height / 13, self.view.frame.size.width, self.view.frame.size.height)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = YES;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
#pragma mark 设置返回按钮
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 530, 320, 40)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
#pragma mark 创建取消button
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(0, self.view.frame.size.height - 45, 50, 50);
    [cancelButton addTarget:self action:@selector(button_backClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:cancelButton];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.articlesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"image"];
    if (cell == nil) {
        cell = [[ImageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"image"];
    }
    
    webObject *web = self.dataArray[indexPath.row];
    NSArray *arr = [self.articlesArr[indexPath.row] objectForKey:@"media"];
    
    NSDictionary *dic = arr[0];
    self.height = [[dic objectForKey:@"h"] intValue];
    cell.webHight = self.height;
    cell.titleLabel.text = web.title;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:web.myImageView]];
    cell.authLabel.text = web.auther_name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [self.articlesArr[indexPath.row] objectForKey:@"media"];
    if (arr.count != 0) {
        NSDictionary *dic = arr[0];
        self.height = [[dic objectForKey:@"h"] intValue];
    }
    return self.height / 2 + 80;
}

- (void)button_backClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
